<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>postMessage-user0</title>
  <style>
    *{
      margin: 0;
      padding: 0;
    }
    .container{
      width: 1000px;
      height: 700px;
      margin: 10% auto;
    }
  </style>
</head>
<body>
  <div id="root">
    <div class="container">
      <div class="main">
        <div class="item" :for="item in otherMessage" :key="item.id">{{item.message}}</div>
      </div>
      <div class="bottom">
        <button v-if="!this.origin" @click="open">开启新页面通信</button>
        <button @click="sendMessage">发送</button>
      </div>
    </div>
  </div>
  <script type="module">
    import {Component} from '../core/index.js'
    const component = new Component({
        data() {
          return {
            origin: null,
            otherMessage: [{id: 1, message: 123}]
          };
        },
        watch: {
        },
        created() {
          window.addEventListener('message', e => {
            console.log(e.data);
            this.otherMessage.push(e.data)
          })
        },
        mounted() {
        },
        methods: {
          open() {
            this.origin = window.open('./user1.html', '_blank')
          },
          sendMessage() {
            if (!this.origin) return
            this.origin.postMessage({
              id: Date.now(),
              message: `这是第${this.otherMessage.length}条消息`,
              user: 0,
              target: 1
            })
          }
        }
    });
    component.mount('#root')
  </script>
</body>
</html>